
import 'package:im_app/im/binary/FieldTypeInterface.dart';
import 'package:json_annotation/json_annotation.dart';

import '../../../util/DateUtil.dart';
import 'Dto.dart';

part 'Resp.g.dart';

class Resp extends Dto {
  DateTime successDataTime;     // 消息发送成功时间
  String? localSeq;

  Resp({this.localSeq, required this.successDataTime});

  @override
  String getLocalSeq() {
    return localSeq??'';
  }
}

@JsonSerializable()
class OkResp extends Resp implements FieldTypeInterface {
  int coder;
  String? otherInfo;

  OkResp({required this.coder, required super.successDataTime, super.localSeq});

  bool isSuccess() {
    return coder == 0;
  }

  factory OkResp.fromJson(Map<String, dynamic> json) => _$OkRespFromJson(json);

  Map<String, dynamic> toJson() => _$OkRespToJson(this);

  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};
    var coderWrapper = FieldWrapper(FieldType.short, (OkResp resp)=>resp.coder, (OkResp resp,
        int? coder)=> resp.coder = coder??0);
    map["coder"] = coderWrapper;

    var otherInfoWrapper = FieldWrapper(FieldType.string, (OkResp resp)=>resp.otherInfo, (OkResp resp,
        String? otherInfo)=> resp.otherInfo = otherInfo??'');
    map["otherInfo"] = otherInfoWrapper;

    var successDataTimeWrapper = FieldWrapper(FieldType.date, (OkResp resp)=>resp.successDataTime, (OkResp resp,
        DateTime? successDataTime)=> resp.successDataTime = successDataTime??DateTime.now());
    map["successDataTime"] = successDataTimeWrapper;

    var localSeqWrapper = FieldWrapper(FieldType.string, (OkResp resp)=>resp.localSeq, (OkResp resp,
        String? localSeq)=> resp.localSeq = localSeq??'');
    map["localSeq"] = localSeqWrapper;

    return map;
  }
}

class OkListResp extends OkResp {
  Set<String> localSeqSet;

  OkListResp({required this.localSeqSet, required super.successDataTime, required super.coder});

  factory OkListResp.fromJson(Map<String, dynamic> json) => _$OkListRespFromJson(json);

  Map<String, dynamic> toJson() => _$OkListRespToJson(this);
}

class ExceptionResp implements FieldTypeInterface {
  int errorStatus;
  String? errorMsg;

  ExceptionResp(this.errorStatus, this.errorMsg);

  factory ExceptionResp.fromJson(Map<String, dynamic> json) {
    ExceptionResp exceptionResp = new ExceptionResp(json['errorStatus'], json['errorMsg']);
    return exceptionResp;
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
    'errorStatus': errorStatus,
    'errorMsg': errorMsg,
  };

  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};
    var errorStatusWrapper = FieldWrapper(FieldType.int, (ExceptionResp resp)=>resp.errorStatus, (ExceptionResp resp,
        int? errorStatus)=> resp.errorStatus = errorStatus??0);
    map["errorStatus"] = errorStatusWrapper;

    var errorMsgWrapper = FieldWrapper(FieldType.string, (ExceptionResp resp)=>resp.errorMsg, (ExceptionResp resp,
        String? errorMsg)=> resp.errorMsg = errorMsg??'');
    map["errorMsg"] = errorMsgWrapper;
    return map;
  }
}